﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Gao.Poker.Domain;
using Gao.Poker.Services;
using Gao.Poker.ViewModel;

namespace Gao.Poker.App.Win
{
    /// <summary>
    /// Interaction logic for BuyStakeWin.xaml
    /// </summary>
    public partial class BuyStakeWin : Window
    {
        public CompetitionViewModel ViewModel { get; set; }
        public Competition Competition { get; set; }

        public BuyStakeWin()
        {
            InitializeComponent();

            btnSearch.Click += Search_Click;
        }

        void Search_Click(object sender, RoutedEventArgs e)
        {
            MemberService service = new MemberService();
            var list = service.SearchMembers(txtName.Text, txtCardNo.Text);

            dgMembers.ItemsSource = list;
        }

        private void Bubmit_Click(object sender, RoutedEventArgs e)
        {
            Gao.Poker.Domain.Member member = dgMembers.SelectedItem as Gao.Poker.Domain.Member;
            if (member == null)
            {
                MessageBox.Show("请选择报名的会员", "提示信息");
                return;
            }

            if(iuSeveral.Value == null)
            {
                MessageBox.Show("不能买入0份", "提示信息");
                return;
            }

            CompetitionEntryService ces = new CompetitionEntryService();
            var competitionEntry = ces.GetMember(member.CardNo, Competition.ID);
            if(competitionEntry == null)
            {
                MessageBox.Show("当前会员未报名参加比赛", "提示信息");
                return;
            }

            BuyRecord record = new BuyRecord();
            if(rbBuy.IsChecked == true)
            {
                record.BuyType = 0;
                competitionEntry.BuyNumber += iuSeveral.Value.Value;
                competitionEntry.BuyMoney += iuSeveral.Value.Value * Competition.SingleAmount;
                Competition.BuyNumber += iuSeveral.Value.Value;
                Competition.TotalBet += iuSeveral.Value.Value * Competition.SingleAmount;
            } 
            else if (rbBuyAgain.IsChecked == true)
            {
                record.BuyType = 1;
                competitionEntry.BuyAgainNumber += iuSeveral.Value.Value;
                competitionEntry.BuyMoney += iuSeveral.Value.Value * Competition.SingleAmount;
                Competition.BuyAgainNumber += iuSeveral.Value.Value;
                Competition.TotalBet += iuSeveral.Value.Value * Competition.SingleAmount;
            }
            else if(rbBuyLast.IsChecked == true)
            {
                record.BuyType = 2;
                competitionEntry.BuyLastNumber += iuSeveral.Value.Value;
                competitionEntry.BuyMoney += iuSeveral.Value.Value * Competition.SingleAmount * 2;
                Competition.BuyLastNumber += iuSeveral.Value.Value;
                Competition.TotalBet += iuSeveral.Value.Value * Competition.SingleAmount * 2;
            }

            record.CardNo = member.CardNo;
            record.ConsumptionID = Competition.ID;
            record.Several = iuSeveral.Value.Value;
            record.CreateDt = DateTime.Now;
           
            BuyRecordService service = new BuyRecordService();
            service.Save(record);
            CompetitionService cs = new CompetitionService();
            Competition = cs.Save(Competition);

            ces.Save(competitionEntry);

            ViewModel.OnPropertyChanged("Members");
            ViewModel.OnPropertyChanged("Competition");
            Close();
        }
    }
}
